﻿
package com.example.videooncarmera;

import sc.tool.audio.AudioTool.AudioPlayer;
import sc.tool.video.CameraSurfaceView;
import sc.tool.video.CameraSurfaceView.CallBack_Bitmap;
import sci.tool.ActivityComponent;
import sci.tool.SocketCallBack;
import sci.tool.SocketClient;
import sci.tool.ThreadTool;
import sci.tool.ThreadTool.ThreadPram;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;


/** Android Socket 演示demo主界面 */
public class MainActivity extends ActivityComponent
{
	
	private void getScreenSize(Context context)
	{
		DisplayMetrics metrics = context.getResources().getDisplayMetrics();
		
		String msg = "屏幕分辨率" + metrics.widthPixels + " x " + metrics.heightPixels;
		Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
	}
	
	SocketClient msgClient = null;		// 用于聊天信息、命令传输
	AudioPlayer audioPlayer;	// 音频播放器
	
	/** 设置界面显示 */
	@Override
	public void Init(Bundle savedInstanceState)
	{
		this.setContentView("activity_main");
		
		getScreenSize(this);
		
		CameraSurfaceView surView = new CameraSurfaceView(this, new CallBack_Bitmap()
		{
			@Override
			public void F(Bitmap pic)
			{
				ImageView("imageView_pre").setImageBitmap(pic);
				
//				// 这里将实时预览的帧率图像发送至服务器端
//				if(vedioClient != null && vedioClient.isConnected())
//				{
//					String data = TransCodeTool.ToString(pic);	// 将图片转化为字符串
//					vedioClient.Send(data);
//				}
			}
		});
		
		RelativeLayout("surfaceviewlayout").addView(surView);
		
		audioPlayer = new AudioPlayer();	// 创建AudioPlayer用于音频播放，此处仅演示本地播放
		
		AutoConnect();						// 自动连接
		
//		String Url = "http://www.scimence.cn:8002/Pages/Product.aspx";
//		OpenWebPage(Url, "");
	}
	
	
	/** 设置Socket自动连接(每5秒检测一次连接) */
	private void AutoConnect()
	{
		if(msgClient == null)
		{
			SocketCallBack print = new SocketCallBack()
			{
				@Override
				public void Print(String info)
				{
					DealMsg(info);
				}
			};
			
			String ipString = "132.232.124.176";
			int port = 37281;
			
			msgClient = new SocketClient(print, ipString, port);	// 创建客户端Socket操作对象
		}
		
		if(!msgClient.isConnected())
		{
			msgClient.start();
		}
		
		// 每5秒，自动检测，并连接
		ThreadTool.RunInMainThread(new ThreadPram()
		{
			@Override
			public void Function()
			{
				AutoConnect();
			}
		}, 5000);
	}
	
	/** 按钮点击响应逻辑 */
	@Override
	public void Click(String viewId)
	{
		
//		if (viewId.equals("btn_send"))
//		{
//			String data = EditText("editMsg").getText().toString();
//			msgClient.Send(data);										// 发送信息
//		}
		
		if(viewId.equals("btn_close")) ShowFuncPage(true);
		else if(viewId.equals("btn_OnlineBank"))
		{
			msgClient.Send("[cmd]OnlineBank");
		}
		else if(viewId.equals("btn_CardLost"))
		{
			msgClient.Send("[cmd]CardLost");
			this.ShowToast("此处暂未设置处理逻辑，请看网银注册！");
		}
		else if(viewId.equals("btn_Password"))
		{
			OpenPasswordPage();
		}
		else if(viewId.equals("webPage_yes"))
		{
			msgClient.Send("[cmd][OpenWebPage]"+ OpenPageUrl + "[btnYes]");
		}
		else if(viewId.equals("webPage_cancel"))
		{
			msgClient.Send("[cmd][OpenWebPage]"+ OpenPageUrl + "[btnCancel]");
		}
		
		
	}
	
	/** 对服务器端发送过来的信息，进行相应的处理 */
	private void DealMsg(final String msg)
	{
		ThreadTool.RunInMainThread(new ThreadPram()
		{
			@Override
			public void Function()
			{
				String cmd = GetStrCMD(msg, "[cmd]");
				if(!cmd.equals("")) DoCMD(cmd);
				else ShowToast(msg);
			}
		});
	}
	
	/** 执行远程服务器端发送的操作命令 */
	private void DoCMD(String cmd)
	{
		if(cmd.startsWith("[OpenWebPage]"))
		{
			String url = GetStrCMD(cmd, "[OpenWebPage]");
			OpenWebPage_Process(url);
		}
		else if(cmd.startsWith("[OpenPasswordPage]"))
		{
			OpenPasswordPage();
		}
	}
	
	/** 展示指定的网页处理逻辑 */
	private void OpenWebPage_Process(String url)
	{
		if(url.contains(";")) 
		{
			String[] A = url.split(";");
			String btnText1 = "";
			String btnText2 = "";
			
			if(A.length > 0) url = A[0].trim();
			if(A.length > 1) btnText1 = A[1].trim();
			if(A.length > 2) btnText2 = A[2].trim();
			
			if(btnText1.equals("") && btnText2.equals("")) 
			{
				LinearLayout("webPage_linerLast").setVisibility(View.INVISIBLE);
			}
			else
			{
				Button("btnText1").setVisibility(btnText1.equals("") ? View.INVISIBLE : View.VISIBLE);
				Button("btnText2").setVisibility(btnText2.equals("") ? View.INVISIBLE : View.VISIBLE);
				
				if(!btnText1.equals("")) Button("btnText1").setText(btnText1);
				if(!btnText2.equals("")) Button("btnText2").setText(btnText2);
			}
		}
		
		OpenWebPage(url, "");
	}
	
	/** 
	 * 获取Str中的操作命令。
	 * 如： GetStrCMD("[cmd]123456", "[cmd]");
	 * 返回：123456
	 * */
	private String GetStrCMD(String Str, String Key)
	{
		int index = Str.indexOf(Key);
		if(index == -1) return "";
		
		String CMD = Str.substring(index + Key.length()).trim();
		return CMD;
	}
	
	/** 以toast形式展示消息 */
	private void ShowToast(final String msg)
	{
		ThreadTool.RunInMainThread(new ThreadPram()
		{
			@Override
			public void Function()
			{
				Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
				// ScrollView("msg_scroll").fullScroll(ScrollView.FOCUS_DOWN);
			}
		});
	}
	
	/** 切换界面至功能展示区 */
	private void ShowFuncPage(boolean showFunc)
	{
		int VISIBLE = showFunc ? View.VISIBLE : View.INVISIBLE;
		int INVISIBLE = showFunc ? View.INVISIBLE : View.VISIBLE;
		
		LinearLayout("func_mainView").setVisibility(VISIBLE);
		LinearLayout("webPage_mainView").setVisibility(INVISIBLE);
		ImageView("btn_close").setVisibility(INVISIBLE);
	}
	
	String dataId = "";
	String OpenPageUrl = "";
	
	/** 在当前页面中展示指定Url页面 */
	private void OpenWebPage(String Url, String dataId)
	{
		ShowFuncPage(false);
		
		this.dataId = dataId;
		this.OpenPageUrl = Url;
		
		WebView("webPage_webView").loadUrl(Url);
	}
	
	/** 打开密码输入界面 */
	private void OpenPasswordPage()
	{
		passwordInput.Show(this);
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (requestCode == passwordInput.CODE_InputBox)
		{
			try
			{
				String value = data.getStringExtra("VALUE").trim(); // 获取输入的股票代码
				msgClient.Send("[cmd][OpenPasswordPage]" + value);		// 发送输入的密码数据到服务器端
				this.ShowToast("输入密码：" + value);
			}
			catch(Exception ex)
			{
				this.ShowToast(ex.toString());
			}
		}
	}
	
}

